//警告弹框
var DIALOG_HEIGHT = 200;
var DIALOG_WIDTH = 500;
var SHOW_MESSAGES = true;

//var IMAGE_PATH = "../../viewer/figures";
var IMAGE_PATH = "/img"; // for testing

var messages = new Array();

if (!top.wcnList) {
top.wcnList = [];
}
var messageCounter = 0;
var PPrompt = {
  alert: function(msg) {
    // Add the overlay    
    this.addOverlay();
    // Get the actual window
    var pwindow = this.getWindow(DIALOG_HEIGHT, DIALOG_WIDTH);
    
    // colored border
    var borderElt = document.createElement('div');
    borderElt.id = "outer";
    if (msg.type == "WARNING") {
      borderElt.className = "warningborder1";
    } else {
      borderElt.className = "cautionborder1";
    }
    pwindow.appendChild(borderElt);
    
    var warningElt = document.createElement('div');
    warningElt.className = "warning";
    borderElt.appendChild(warningElt);
    
    // warning table
    var tableElt = document.createElement('table');
    tableElt.id = "entire";
    tableElt.border = '0';
    tableElt.cellspacing = '0';
    tableElt.cellpadding = '0';
    warningElt.appendChild(tableElt);

    // msg type
    var rowElt = tableElt.insertRow(-1);
    var entryElt = rowElt.insertCell(-1);
    entryElt.align = "center";
    var typeElt = document.createElement('span');
    typeElt.width = '450';
    typeElt.appendChild(document.createTextNode(msg.name));
    typeElt.style.fontWeight = 'bold';
    entryElt.appendChild(typeElt);

    // msg text
    rowElt = tableElt.insertRow(-1);
    entryElt = rowElt.insertCell(-1);
    entryElt.align = "center";
    var textElt = document.createElement('span');
    textElt.width = "450";
    textElt.innerHTML = msg.text;
    entryElt.appendChild(textElt);

    // buttons
    rowElt = tableElt.insertRow(-1);
    entryElt = rowElt.insertCell(-1);
    entryElt.align = "center";
    entryElt.width = "450";
    var buttonsElt = document.createElement('div');
    buttonsElt.id = 'pbuttons';
    buttonsElt.style.background = '#FFF';
    entryElt.appendChild(buttonsElt);

    // close button
    var okButtonElt = document.createElement('input');
    okButtonElt.type = "image";
    okButtonElt.src = IMAGE_PATH + '/b_check.gif';
    okButtonElt.style.width = '22px';
    okButtonElt.style.height = '22px';
    okButtonElt.onclick = function() { PPrompt.close(); }
    okButtonElt.onmouseover = function() { okButtonElt.src = IMAGE_PATH + '/b_check_roll.gif'};
    okButtonElt.onmouseout = function() { okButtonElt.src = IMAGE_PATH + '/b_check.gif'};
    okButtonElt.onmousedown = function() { okButtonElt.src = IMAGE_PATH + '/b_check_pressed.gif'};
    okButtonElt.onmouseup = function() { okButtonElt.src = IMAGE_PATH + '/b_check.gif'};
    buttonsElt.appendChild(okButtonElt);
    
    pwindow.style.height = borderElt.style.height;
    document.body.appendChild(pwindow);
    var pageSize = this._getPageSize();
    if (pageSize.windowHeight > pwindow.clientHeight) {
      pwindow.style.top = (pageSize.windowHeight/2 - pwindow.clientHeight/2)+'px';
    } else {
      pwindow.style.top = '0px';
    }
    okButtonElt.focus();
  },
  addOverlay: function() {
    var poverlay = document.createElement('div');
    poverlay.id = 'poverlay';
    poverlay.style.top = '0px';
    poverlay.style.left = '0px';
    poverlay.style.position = 'absolute';
    poverlay.style.background = '#000';

    this._setOpacity(poverlay, 0.5);
    var pageSize = this._getPageSize();
    poverlay.style.height = pageSize.pageHeight+'px';
    poverlay.style.width = '100%';
    document.body.appendChild(poverlay);
  },
  removeOverlay: function() {
    document.body.removeChild(document.getElementById('poverlay'));
  },
  getWindow: function(height, width) {
    document.body.style.padding = '0';
    var pwindow = document.createElement('div');
    pwindow.id = 'pwindow';
    var pageSize = this._getPageSize();
    var pos = this._realOffset(document.body);
    pwindow.style.top = (pageSize.windowHeight/2 - height/2 + pos[1])+'px';
    pwindow.style.left = (pageSize.windowWidth/2 - width/2 + pos[0])+'px';
    pwindow.style.height = height+'px';
    pwindow.style.width = width+'px';
    pwindow.style.position = 'absolute';
    pwindow.style.background = '#fff';
    pwindow.style.border = '2px solid #222';
    pwindow.style.textAlign = 'center';
    pwindow.style.fontFamily = 'verdana, arial, san-serif';
    return pwindow;
  },
  close: function() {
    this.removeOverlay();
    document.body.removeChild(document.getElementById('pwindow'));
    // Specialty : show the next message if there is any
	var msg = messages[messageCounter];
		// Add id to list of already displayed messages
	//top.wcnList[top.wcnList.length] = msg.id;
    messageCounter++;

    while (messages.length > messageCounter) {
		var msg = messages[messageCounter];
		if(true){
			//top.wcnList[top.wcnList.length] = msg.id;
			PPrompt.alert(messages[messageCounter]);
			break;
		}
		messageCounter++;
    }
  },
  _realOffset: function(element) {
    var valueT = 0, valueL = 0;
    do {
      valueT += element.scrollTop  || 0;
      valueL += element.scrollLeft || 0;
      element = element.parentNode;
    } while (element);
    return [valueL, valueT];
  },
  _setOpacity: function(element, value){
    if (typeof element == 'string')
      element= $(element);
    if (value == 1){
      element.style.opacity = (/Gecko/.test(navigator.userAgent) && !/Konqueror|Safari|KHTML/.test(navigator.userAgent)) ? 0.999999 : 1.0 ;
      if(/MSIE/.test(navigator.userAgent) && !window.opera)
        element.style.filter = element.style.filter.replace(/alpha\([^\)]*\)/gi,'');
    } else {
      if(value < 0.00001) value = 0;
      element.style.opacity = value;
      if(/MSIE/.test(navigator.userAgent) && !window.opera)
        element.style.filter = element.style.filter.replace(/alpha\([^\)]*\)/gi,'') + 'alpha(opacity='+value*100+')';
    }
    return element;
  },
  _getPageSize: function() {
    var xScroll, yScroll;
    if (window.innerHeight && window.scrollMaxY) {
      xScroll = document.body.scrollWidth;
      yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){
      // all but Explorer Mac
      xScroll = document.body.scrollWidth;
      yScroll = document.body.scrollHeight;
    } else {
      // Explorer Mac...would also work in Explorer 6 Strict,
      // Mozilla and Safari
      xScroll = document.body.offsetWidth;
      yScroll = document.body.offsetHeight;
    }

    var windowWidth, windowHeight;
    if (self.innerHeight) {      // all except Explorer
      windowWidth = self.innerWidth;
      windowHeight = self.innerHeight;
    } else if (document.documentElement
    && document.documentElement.clientHeight) {
      // Explorer 6 Strict Mode
      windowWidth = document.documentElement.clientWidth;
      windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
      windowWidth = document.body.clientWidth;
      windowHeight = document.body.clientHeight;
    }

    // for small pages with total height less then height of the viewport
    if(yScroll < windowHeight){
      pageHeight = windowHeight;
    } else {
      pageHeight = yScroll;
    }

    // for small pages with total width less then width of the viewport
    if(xScroll < windowWidth){
      pageWidth = windowWidth;
    } else {
      pageWidth = xScroll;
    }

    return {
      'pageWidth':pageWidth,
      'pageHeight':pageHeight,
      'windowWidth':windowWidth,
      'windowHeight':windowHeight,
      'yScroll':yScroll,
      'xScroll':xScroll
    };
  }
};

/*
 * Displays the messages stored in the array
 */
function displayMessages() {

  if (messages.length > 0) {
    // Show the first message
//console.log(messages);
		var msg = messages[messageCounter];
		if(true){
			//console.log(top.wcnList);
			//top.wcnList[top.wcnList.length] = msg.id;
			PPrompt.alert(messages[messageCounter]);
		}
  }
}

/*
 * Adds a message to the array
 */
function addMessage(msg) {

  if (SHOW_MESSAGES) {
	  
    messages[messages.length] = msg;
  }
}

/*
*  Check to see if the messages were previously displayed
*/
function contains(arr, id) {
	var first = false;
	var i = arr.length;
	while(i--) {
		if(arr[i] == id) {
			first = true;
			break;
		}
	}
	return first; 
}